package rmsstream;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.media.Player;
import javax.microedition.media.control.VideoControl;

import midlets.MediaPlayer;

public class VideoCanvas extends Canvas implements Runnable, CommandListener {

	private Player player;
	private int width, height;
	private Command done;

	public VideoCanvas(Player player) {
		this.player = player;
		width = getWidth();
		height = getHeight();
		done = new Command("Done", Command.OK, 1);
		addCommand(done);
		setCommandListener(this);
		// this.setFullScreenMode(true);
	}

	public void startPlayer() {
		

		try {
			VideoControl video = (VideoControl) player
					.getControl("VideoControl");
			video.initDisplayMode(VideoControl.USE_DIRECT_VIDEO, this);

			video.setDisplayLocation(2, 2);
			video.setDisplaySize(height - 4, width - 4);
			// video.setDisplayFullScreen(true);
			video.setVisible(true);
			System.out
					.println("================ starting new player  ===============");
			player.start();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void paint(Graphics g) {

		// Draw a green border around the VideoControl.
		g.setColor(0x00ff00);
		g.drawRect(0, 0, width - 1, height - 1);
		g.drawRect(1, 1, width - 3, height - 3);
	}

	public void commandAction(Command c, Displayable s) {
		if (c == done) {
			MediaPlayer.stopPlayer();

		}
	}

	public void run() {
		System.out.println("VIDEOCANVAS> startPlayer Thread");
		startPlayer();
	}
}
